package com.cwm.test.flyweight;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
 
public class VectorExample {
    public static void main(String[] args) {
        List<Integer> vectorList = new CopyOnWriteArrayList<>();
 
        // 添加元素的线程
        Thread addThread = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                vectorList.add(i);
            }
        });
 
        // 移除元素的线程
        Thread removeThread = new Thread(() -> {
            try {
                addThread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < vectorList.size(); ) {
                vectorList.remove(i);
            }
        });
 
        // 启动线程
        addThread.start();
        removeThread.start();

        // 打印最终的列表
        System.out.println(vectorList);
    }
}